package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.DailyLog;
import model.bean.Service;
import model.bean.ServiceInfo;
import model.bean.TourInfo;
import model.bean.TourKind;
import model.bean.TourTemp;
import model.bean.TourTemplate;
import model.bo.TourUpdateBO;

/**
 * Servlet implementation class TourUpdateServlet
 */
public class TourUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TourUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("process");
		TourUpdateBO bo = null;
		
		
		if ("dailyPanel".equals(action)) {
			bo = new TourUpdateBO();
			ArrayList<TourKind> kind = bo.GetTourKind();

			request.setAttribute("kind", kind);
			RequestDispatcher rd = request.getRequestDispatcher("manage_tour/dailypanel.jsp");
			rd.forward(request, response);
		}
		
		//add new template tour
		if ("Add".equals(action)) {
			bo = new TourUpdateBO();
			
			//get parametter
			String tourTempName = request.getParameter("tourTempName");
			int tourKind = Integer.parseInt(request.getParameter("tourKind").trim());
			String priceStr = request.getParameter("tourPrice").trim();
			double tourPrice = Double.parseDouble(priceStr!=null?priceStr:"0");
			int usual = Integer.parseInt(request.getParameter("usual").trim());
			
			//process
			boolean insert = false;
			insert = bo.insertTour(tourTempName, tourKind, tourPrice, usual);
			ArrayList<TourKind> kind = bo.GetTourKind();
			
			//result
			String insertRs = "";
			insertRs = insert==true?"them tour thanh cong":"them tour that bai";
			
			request.setAttribute("kind", kind);
			request.setAttribute("insertRs", insertRs);
			RequestDispatcher rd = request.getRequestDispatcher("manage_tour/dailypanel.jsp");
			rd.forward(request, response);
		}
		
		// load list template tours
		if ("Xem Tour Mau".equals(action)){
			bo = new TourUpdateBO();
			//load tour
			ArrayList<TourTemp> listTour = null;
			listTour = bo.getListTour();
			
			request.setAttribute("listTour", listTour);
			RequestDispatcher rd = request.getRequestDispatcher("manage_tour/dailypanel.jsp");
			rd.forward(request, response);
		}
		// template tour modification
		if("Modify".equals(action)){
			bo = new TourUpdateBO();
			int tourId = Integer.parseInt(request.getParameter("tourId").trim());
			
			ArrayList<TourKind> tour = bo.GetTourKind();
			request.setAttribute("tourKind", tour);
			RequestDispatcher rd = request.getRequestDispatcher("manage_tour/edittour.jsp?id="+tourId);
			rd.forward(request, response);
		}
		
		//apply the modification
		if ("Change".equals(action)) {
			bo = new TourUpdateBO();
			int tourTempIndex = Integer.parseInt(request.getParameter("tourId").trim());
			String tourTempName = request.getParameter("tourTempName");
			int tourKind = Integer.parseInt(request.getParameter("tourKind").trim());
			String priceStr = request.getParameter("tourPrice").trim();
			double tourPrice = Double.parseDouble(priceStr!=null?priceStr:"0");
			int usual = Integer.parseInt(request.getParameter("usual").trim());
			
			//process
			boolean update = bo.updateTour(tourTempIndex, tourTempName, tourKind, tourPrice, usual );
			String updateRs = update==true?"Cap nhat thanh cong":"cap nhat that bai";
			
			ArrayList<TourTemp> listTour = null;
			listTour = bo.getListTour();
			
			request.setAttribute("updateRs", updateRs);
			request.setAttribute("listTour", listTour);
			RequestDispatcher rd = request.getRequestDispatcher("manage_tour/dailypanel.jsp");
			rd.forward(request, response);
		}
		if ("dailyLog".equals(action)) {
			//parameter
			String tourIdStr = request.getParameter("tourId");
			int tourId = Integer.parseInt(tourIdStr.trim());
			bo = new TourUpdateBO();
			ArrayList<DailyLog> dailyLog = bo.getDailyLog(tourId);
			
			//get List service
			ArrayList<Service> hotel = bo.getService(1);
			ArrayList<Service> res = bo.getService(2);
			ArrayList<Service> traffic = bo.getService(3);
			ArrayList<Service> target = bo.getService(4);
			
			//send to client
			request.setAttribute("hotel", hotel);
			request.setAttribute("res", res);
			request.setAttribute("traffic", traffic);
			request.setAttribute("target", target);
			
			request.setAttribute("dailyLog", dailyLog);
			
			
			RequestDispatcher rd = request.getRequestDispatcher("manage_tour/dailylog.jsp?id="+tourId);
			rd.forward(request, response);
			
		}
		if ("updateLog".equals(action)){
			//get parameter
			String targetStr = request.getParameter("target");
			String trafficStr = request.getParameter("traffic");
			String resStr = request.getParameter("res");
			String hotelStr = request.getParameter("hotel");
			String tourTmpIdStr = request.getParameter("tourId");
			
			//convert parametter
			int target = Integer.parseInt(targetStr.trim());
			int traffic = Integer.parseInt(trafficStr.trim());
			int res = Integer.parseInt(resStr.trim());
			int hotel = Integer.parseInt(hotelStr.trim());
			int day = 0;
			String tourIdStr = request.getParameter("tourId");
			int tourId = Integer.parseInt(tourIdStr.trim());
			
			//process
			bo = new TourUpdateBO();
			
			//set Attribute
			ArrayList<TourKind> kind = bo.GetTourKind();

            request.setAttribute("kind", kind);
			ArrayList<DailyLog> dailyLog = bo.getDailyLog(tourId);
			boolean dailyInsertRs = bo.insertDailyLog(tourId, target, traffic, res, hotel, day);
			String dailyInsert = dailyInsertRs==true?"da cap nhat nhat trinh":"cap nhat nhat trinh that bai";
			
			request.setAttribute("dailyInsert", dailyInsert);
			request.setAttribute("dailyLog", dailyLog);
			RequestDispatcher rd = request.getRequestDispatcher("manage_tour/dailypanel.jsp");
			rd.forward(request, response);
		}
		if ("tourMng".equals(action)) {
		    System.out.println(action);
		    RequestDispatcher rd = request.getRequestDispatcher("manage_tour/tourmngpanel.jsp");
            rd.forward(request, response);
		}
		if ("Check Tour Info".equals(action)) {
		  //get elements
            String dayFirst = request.getParameter("tfDayComing");
            String dayUp = request.getParameter("tfDayEnding");
            bo = new TourUpdateBO();
            ArrayList<TourInfo> listTour = bo.getTourList(dayFirst,dayUp);
            
            //send result
            RequestDispatcher rd = request.getRequestDispatcher("manage_tour/tourmngpanel.jsp");
            request.setAttribute("listTour", listTour);
            rd.forward(request, response);
		}
		if ("updateTour".equals(action)) {
            bo = new TourUpdateBO();
            ArrayList<TourTemplate> listTempTour = bo.getTempList();
            System.out.println(action);
            RequestDispatcher rd = request.getRequestDispatcher("manage_tour/tourupdatepanel.jsp?index="+request.getParameter("index"));
            request.setAttribute("listTempTour", listTempTour);
            rd.forward(request, response);
        }
		if ("Excute Update Tour".equals(action)) {
		    bo = new TourUpdateBO();
            //TourInfo tour = bo.getTour();
            
		    //get new infor.
            int tourTempIndex = Integer.parseInt(request.getParameter("tourTempIndex").trim());
            String dayStart = request.getParameter("dayStart");
            String dayEnd = request.getParameter("dayEnd");
            int tourIndex = 0;
            tourIndex = Integer.parseInt(request.getParameter("tourIndex").trim());
            System.out.println(dayStart+" "+tourIndex);
            boolean excuteUpdateTour = bo.updateTourExac(tourIndex, tourTempIndex, dayStart, dayEnd);
            System.out.println(excuteUpdateTour);
         
            ArrayList<TourInfo> listTour = bo.getTourList("10-10-1993","12-12-2112");
            request.setAttribute("listTour", listTour);
            String resultUpdate = excuteUpdateTour?"Cap nhat thanh cong":"cap nhat that bai"; 
            request.setAttribute("resultUpdate", resultUpdate);
            
            RequestDispatcher rd = request.getRequestDispatcher("manage_tour/tourmngpanel.jsp");
            rd.forward(request, response);
		}
		if ("Service Statistic".equals(action)) {
	            bo = new TourUpdateBO();
	            int type = Integer.parseInt(request.getParameter("object").trim());
	            String dayStart = request.getParameter("tfDayStart");
	            String dayEnd = request.getParameter("tfEndDay");
	            int odd = Integer.parseInt(request.getParameter("order"));
	            String order = odd==1?"ASC":"DESC";
	            
	            ArrayList<ServiceInfo> listService = bo.getServiceList(type, dayStart, dayEnd, order);
	            for(int i = 0; i < listService.size(); i++) {
	                System.out.println(listService.get(i).getSvName());
	            }
	            request.setAttribute("listService",listService);
	            RequestDispatcher rd = request.getRequestDispatcher("manage_tour/servicestatistic.jsp");
	            rd.forward(request, response);
	            
	         
		}
		if ("ServicePanel".equals(action)) {
		    RequestDispatcher rd = request.getRequestDispatcher("manage_tour/servicestatistic.jsp");
            rd.forward(request, response);
		}
		if ( action != null ) {
		    System.out.println(action);
		}
	}

}
